
package com.shop.cloud.mall.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.mall.api.service.UserBalanceService;
import com.shop.cloud.mall.common.entity.UserBalanceRecord;
import com.shop.cloud.mall.api.mapper.UserBalanceRecordMapper;
import com.shop.cloud.mall.api.service.UserBalanceRecordService;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户余额记录
 *
 * @author 
 * @date 2023-11-01 10:21:44
 */
@Service
@AllArgsConstructor
public class UserBalanceRecordServiceImpl extends ServiceImpl<UserBalanceRecordMapper, UserBalanceRecord> implements UserBalanceRecordService {
    private final UserBalanceService userBalanceService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    //加锁防止支付平台同时发多次回调
    @Klock(name = "notifyUserBalanceRecharge", keys = {"#userBalanceRecord.getId()"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public void notifyUserBalanceRecharge(UserBalanceRecord userBalanceRecord) {
        if(CommonConstants.NO.equals(userBalanceRecord.getIsPay())) {//只有未支付订单能操作
            //更新用户余额
            userBalanceService.updateTotalAmount(userBalanceRecord.getUserId(), userBalanceRecord.getAmount());
            //更新余额记录支付状态
            userBalanceRecord.setIsPay(CommonConstants.YES);
            super.updateById(userBalanceRecord);
        }
    }
}
